import { Application } from 'egg';

export default (app: Application) => {
  const { DataTypes: { STRING, DATE, INTEGER } } = app.Sequelize;

  const Model = app.model.define('category', {
    id: { // 主键
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: { // 名称
      type: STRING(100)
    },
    parentId: { // 父级id
      type: INTEGER,
      references: {
        model: 'categories', // 指定参照的模型
        key: 'id' // 指定参照模型的主键
      }
    },
    createdAt: {
        type: DATE,
        allowNull: true,
        field: 'created_at',
        comment: '创建时间',
      },
      updatedAt: {
        type: DATE,
        allowNull: true,
        field: 'updated_at',
        comment: '修改时间',
      },
  },
  {
    comment: '分类信息',
    tableName: 'category',
  });

  return class CategoryModel extends Model {
    id?: number;
    name?: string;
    parentId?: number;
    createdAt?: Date;
    updatedAt?: Date;
  };
};